part of '../widgets.dart';

enum VanTagType { defaults, primary, success, warning, danger }

enum VanTagSize { large, medium }

class VanTag extends StatelessWidget {
  /// props - 类型，可选值为 primary success danger warning(默认: default)
  final VanTagType type;

  /// props - 大小, 可选值为 large medium
  final VanTagSize? size;

  /// props - 标签颜色
  final Color? color;

  /// props - 是否展示标签(默认: true)
  final bool show;

  /// props - 是否为空心样式(默认: false)
  final bool plain;

  /// props - 是否为圆角样式(默认: false)
  final bool round;

  /// props - 是否为标记样式(默认: false)
  final bool mark;

  /// props - 文本颜色，优先级高于 color 属性(默认: white)
  final Color? textColor;

  /// props - 是否为可关闭标签(默认: false)
  final bool closeable;

  final VanTagStyle? style;

  /// events - 点击时触发
  final void Function()? onClick;

  /// events - 关闭标签时触发
  final void Function()? onClose;

  /// slots - 标签显示内容<br>
  /// types - [Widget] or [String] or [int] or [double]
  final dynamic child;

  const VanTag({
    super.key,
    this.type = VanTagType.defaults,
    this.size,
    this.color,
    this.show = true,
    this.plain = false,
    this.round = false,
    this.mark = false,
    this.textColor,
    this.closeable = false,
    this.style,
    this.onClick,
    this.onClose,
    this.child = "标签",
  });

  VanTagStyle getComputedStyle(BuildContext context) {
    if (style?.isNonNull == true) return style!;
    EdgeInsets? padding = style?.padding;
    Color? textColor = this.textColor ?? style?.textColor;
    double? fontSize = style?.fontSize;
    Border? border = style?.border;
    BorderRadius? radius = style?.radius;
    double? lineHeight = style?.lineHeight;
    VanBgColor? background = style?.background;

    if (color != null) background = VanBgColor(color);

    if (round) {
      radius ??= kVanTagRoundRadius.toBorderRadius(context);
    }

    if (size == VanTagSize.medium) {
      padding ??= kVanTagMediumPadding.toEdgeInsets(context);
    } else if (size == VanTagSize.large) {
      padding ??= kVanTagLargePadding.toEdgeInsets(context);
      radius ??= kVanTagLargeRadius.toBorderRadius(context);
      fontSize ??= kVanTagLargeFontSize.toDouble(context);
    }

    if (type == VanTagType.primary) {
      background ??= kVanTagPrimaryColor.toBgColor(context);
    } else if (type == VanTagType.success) {
      background ??= kVanTagSuccessColor.toBgColor(context);
    } else if (type == VanTagType.warning) {
      background ??= kVanTagWarningColor.toBgColor(context);
    } else if (type == VanTagType.danger) {
      background ??= kVanTagDangerColor.toBgColor(context);
    } else {
      background ??= kVanTagDefaultColor.toBgColor(context);
    }

    if (plain) {
      if (style?.textColor == null) textColor = background?.firstColor;
      if (style?.border == null) border = Border.all(color: textColor ?? Colors.transparent);
      if (style?.background == null) background = kVanTagPlainBackground.toBgColor(context);
    } else {
      border ??= Border.all(color: background?.firstColor ?? Colors.transparent);
    }

    if (mark && style?.radius == null) {
      radius = kVanTagRoundRadius.toBorderRadius(context);
      radius = BorderRadius.horizontal(right: radius?.topRight ?? Radius.zero);
    }

    padding ??= kVanTagPadding.toEdgeInsets(context);
    textColor ??= kVanTagTextColor.toColor(context);
    fontSize ??= kVanTagFontSize.toDouble(context);
    radius ??= kVanTagRadius.toBorderRadius(context);
    lineHeight ??= kVanTagLineHeight.toDouble(context);

    textColor ??= Colors.white;

    return VanTagStyle(
      padding: padding,
      textColor: textColor,
      fontSize: fontSize,
      border: border,
      radius: radius,
      lineHeight: lineHeight,
      background: background,
    );
  }

  @override
  Widget build(BuildContext context) {
    final style0 = getComputedStyle(context);

    Widget? child0;
    if (child is int || child is double) {
      child0 = Text(child.toString());
    } else if (child is String) {
      child0 = Text(child);
    } else if (child is Widget) {
      child0 = child;
    } else if (child != null) {
      _logger.e("VanTag.child only type support [String] or [Widget] or [int] or [double]");
    }

    if (child0 != null) {
      child0 = DefaultTextStyle.merge(
        style: TextStyle(
          color: style0.textColor,
          fontSize: style0.fontSize,
          height: calcLineHeight(lineHeight: style0.lineHeight, fontSize: style0.fontSize),
        ),
        child: child0,
      );
    }

    return Container(
        padding: style0.padding,
        decoration: BoxDecoration(
          color: style0.background?.color,
          gradient: style0.background?.gradient,
          borderRadius: style0.radius,
          border: style0.border,
        ),
        child: child0);
  }
}
